package com.example.rbacmanage.service.impl;

import com.example.rbacmanage.entity.AppUser;
import com.example.rbacmanage.entity.Chat;
import com.example.rbacmanage.entity.User;
import com.example.rbacmanage.exception.MyException;
import com.example.rbacmanage.mapper.AppUserMapper;
import com.example.rbacmanage.mapper.ChatJoinMapper;
import com.example.rbacmanage.mapper.ChatMapper;
import com.example.rbacmanage.mapper.UserMapper;
import com.example.rbacmanage.service.ChatService;
import com.example.rbacmanage.vo.MyUserDetails;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 聊天服务实现类
 *
 * @author zmd
 */
@Service
public class ChatServiceImpl  implements ChatService {
    @Autowired
    private ChatMapper  chatMapper;
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ChatJoinMapper chatJoinMapper;

    @Override
    public void save(Chat chat) {
        chat.setCreateTime(new Date());
        chatMapper.insert(chat);
    }

    @Override
    public void remove(Long[] ids) {
        for(Long id:ids){
                chatMapper.del(id);
        }
    }

    @Override
    public void update(Chat chat) {
            chatMapper.update(chat);
    }

    @Override
    public List<Chat> getAll(Chat chat) {
        List<Chat> list = chatMapper.list(chat);
        for (Chat cha:list){
            if (cha.getSendRole().equals(0)){
                User user = userMapper.queryOneById(cha.getSystemUserId());
                if (user != null){
                    cha.setAvatar(user.getAvatar());
                    cha.setNickName(user.getNickName());
                }
            }else if (cha.getSendRole().equals(1)){
                AppUser user=appUserMapper.queryOneById(cha.getMinappUserId());
                if (user != null){
                    cha.setAvatar(user.getAvatar());
                    cha.setNickName(user.getNickName());
                }
            }
        }
        return list;
    }

    @Override
    public PageInfo<Chat> getPage(Chat chat) {
        PageHelper.startPage(chat.getPage(), chat.getLimit());
        return new PageInfo<>(getAll(chat));
    }

    @Override
    public Chat getById(Long id) {
        return chatMapper.queryOneById(id);
    }

    @Override
    public Integer getCount(Chat chat) {
        return chatMapper.count(chat);
    }

    @Override
    public void checkRepeat(Chat chat) throws MyException {

    }

    @Override
    public PageInfo<Chat> getUsers(Chat chat) {
        PageHelper.startPage(chat.getPage(), chat.getLimit());
        //只获取发送给自己的消息用户列表
        MyUserDetails myUserDetails = (MyUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        chat.setSystemUserId(myUserDetails.getId().longValue());
        List<Chat> chats = chatMapper.selectUsers(chat);
        for (Chat c:chats){
            AppUser user=appUserMapper.queryOneById(c.getMinappUserId());
            if (user != null){
                c.setAvatar(user.getAvatar());
                c.setNickName(user.getNickName());
            }
            Chat cat = chatMapper.queryOneById(c.getId());
            if (cat != null){
                c.setSystemUserId(cat.getSystemUserId());
                c.setLastMsg(cat.getMsg());
                //是否还有未读消息
                Chat query = new Chat();
                query.setMinappUserId(c.getMinappUserId());
                query.setSystemUserId(cat.getSystemUserId());
                query.setRead("0");
                Integer count = chatMapper.count(query);
                if (count > 0){
                    c.setNoRead(true);
                }else{
                    c.setNoRead(false);
                }
            }
            //判断当前系统登录用户是否接入了该客户聊天
            if (c.getJoinedId() != null){
                c.setJoined(true);
            }else{
                c.setJoined(false);
            }
        }
        return new PageInfo<>(chats);
    }
}
